﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Core;
using word = Microsoft.Office.Interop.Word;
using System.IO;
using System.Text.RegularExpressions;
using ReportModel;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;

namespace ReportModel
{
    public class Generator
    {

        /// <summary> 
        /// 计算本周的周一日期 
        /// </summary> 
        /// <returns></returns> 
        public static DateTime GetMondayDate()
        {
            return GetMondayDate(DateTime.Now);
        }
        /// <summary> 
        /// 计算本周周日的日期 
        /// </summary> 
        /// <returns></returns> 
        public static DateTime GetSundayDate()
        {
            return GetSundayDate(DateTime.Now);
        }
        public static DateTime GetDateByIndex(int i)
        {
            return GetMondayDate().AddDays(i-1);
        }

        /// <summary> 
        /// 计算某日起始日期（礼拜一的日期） 
        /// </summary> 
        /// <param name="someDate">该周中任意一天</param> 
        /// <returns>返回礼拜一日期，后面的具体时、分、秒和传入值相等</returns> 
        public static DateTime GetMondayDate(DateTime someDate)
        {
            int i = someDate.DayOfWeek - DayOfWeek.Monday;
            if (i == -1) i = 6;// i值 > = 0 ，因为枚举原因，Sunday排在最前，此时Sunday-Monday=-1，必须+7=6。 
            TimeSpan ts = new TimeSpan(i, 0, 0, 0);
            return someDate.Subtract(ts);
        }
        /// <summary> 
        /// 计算某日结束日期（礼拜日的日期） 
        /// </summary> 
        /// <param name="someDate">该周中任意一天</param> 
        /// <returns>返回礼拜日日期，后面的具体时、分、秒和传入值相等</returns> 
        public static DateTime GetSundayDate(DateTime someDate)
        {

            int i = someDate.DayOfWeek - DayOfWeek.Sunday;
            if (i != 0) i = 7 - i;// 因为枚举原因，Sunday排在最前，相减间隔要被7减。 
            TimeSpan ts = new TimeSpan(i, 0, 0, 0);
            return someDate.Add(ts);
        }

        public static string GeneratorReport(string template,Report report)
        {
             
                FileInfo fileInfo = new FileInfo(template);
                string filename = fileInfo.Name;
                Regex reg = new Regex("(?<=\\()[^\\)]+", RegexOptions.IgnoreCase);


                string newFileName = reg.Replace(filename, report.thisWeek);
                var newPath= fileInfo.DirectoryName+"\\" + newFileName;
                if (File.Exists(newPath))
                {
                    MessageBoxButtons mess = MessageBoxButtons.OKCancel;
                    DialogResult confirm = MessageBox.Show("发现已有文件，是否替换？", "提示", mess);
                    if (confirm== DialogResult.OK)
                    {

                    try
                    {
                        File.Delete(newPath);
                        File.Copy(template, newPath);
                    } catch(Exception exception)
                    {
                        MessageBox.Show(exception.Message);
                        return string.Empty;
                    }
                    finally
                    {

                    }
                        
                        
                    }
                }
                else
                {
                    File.Copy(template, newPath);
                     
                    
                }
                 if(report.editDoc(newPath))
            {
                return newPath;
            }
            return string.Empty;
                
               



        }
       
        
    }
}
